/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tpdisenio2009.model.dao;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import tpdisenio2009.model.HibernateUtils;
import tpdisenio2009.model.beans.FantasyTeam;
import tpdisenio2009.model.beans.FantasyTeamTransferByTournamentDate;
import tpdisenio2009.model.beans.TournamentDate;

/**
 *
 * @author Pablito
 */
public class FantasyTeamTransferByTournamentDateDAO extends AbstractDAO<FantasyTeamTransferByTournamentDate> {

    @Override
    public Class<FantasyTeamTransferByTournamentDate> getPersistentClass() {
        return FantasyTeamTransferByTournamentDate.class;
    }

    public List<FantasyTeamTransferByTournamentDate> getTransfersByFantasyTeam( FantasyTeam team ) {
        Session session = HibernateUtils.getCurrentSession();

        Criteria mainCriteria = session.createCriteria( FantasyTeamTransferByTournamentDate.class );
        mainCriteria.add( Restrictions.eq( "fantasyTeam", team ) );

        return mainCriteria.list();
    }

    public FantasyTeamTransferByTournamentDate getTransfersByFantasyTeamAndTournamentDate( FantasyTeam team, TournamentDate date ) {
        Session session = HibernateUtils.getCurrentSession();

        Criteria mainCriteria = session.createCriteria( FantasyTeamTransferByTournamentDate.class );
        mainCriteria.add( Restrictions.eq( "fantasyTeam", team ) );
        mainCriteria.add( Restrictions.eq( "tournamentDate", date ) );

        return (FantasyTeamTransferByTournamentDate) mainCriteria.uniqueResult();
    }

    public void updateTransfer( FantasyTeamTransferByTournamentDate transfer, int count ) {

        Session session = HibernateUtils.openSession();
        Transaction transaction = session.getTransaction();
        transaction.begin();
        try {
            transfer.setTransfersCount(count);
            session.merge( transfer );
            transaction.commit();
        } catch(HibernateException ex) {
            transaction.rollback();
            throw ex;
        } finally {
            session.close();
        }
    }
}
